package com.demo1.controller;

import com.demo1.entity.Pet;
import com.demo1.entity.R;
import com.demo1.service.IPetService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 宠物Controller
 */
@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;

    @Value("${petImagesFilePath}")
    private String petImagesFilePath;

    /**
     * 查询所有宠物信息
     */
    @GetMapping("/findAll")
    public R findAll(){
        List<Pet> pet = petService.list();
        Map<String,Object> map = new HashMap<>();
        map.put("message",pet);
        return R.ok(map);
    }

    /**
     * 根据id查询宠物详细信息
     */
    @GetMapping("/detail")
    public R detail(@RequestParam(name = "id") int id){
        Pet pet = petService.getById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("message",pet);
        return R.ok(map);
    }

    /**
     * 宠物信息上传
     * @return
     */
    @PostMapping("/upload")
    public R petInfo(@RequestPart("file") MultipartFile file,
                     @RequestPart("pet") String petJson) {
        // Handle the file upload
        if (file != null && !file.isEmpty()) {
            try {
                // Save the file to a specific location or process it as needed
                byte[] bytes = file.getBytes();
                // You can save the bytes to a file, store in the database, etc.
                // For simplicity, let's just print the file name here
                System.out.println("Received file: " + file.getOriginalFilename());
            } catch (IOException e) {
                e.printStackTrace();
                return R.error("Failed to process the file");
            }
        }

        // Handle the pet information (e.g., save to the database)
        // The petJson should contain the JSON representation of the Pet object
        // You need to deserialize it into a Pet object and then save it to the database
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Pet pet = objectMapper.readValue(petJson, Pet.class);
            petService.save(pet);
            return R.ok();
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("Failed to process the pet information");
        }
    }

    @PostMapping("/uploadOne")
    public R uploadOne(@RequestPart("file") MultipartFile file, @RequestPart("petJson") String petJson) {
        // 处理文件上传
        String fileName = null;
        if (file != null && !file.isEmpty()) {
            try {
                // 生成文件存储路径
                fileName = file.getOriginalFilename();
                String filePath = petImagesFilePath + fileName;

                // 将文件保存到指定位置
                File dest = new File(filePath);
                file.transferTo(dest);
                return R.ok(fileName);

            } catch (IOException e) {
                e.printStackTrace();
                return R.error("文件处理失败");
            }
        }
        return R.ok();
    }
}
